using System;
using System.Collections.Generic;
using System.IO;

namespace Gloom
{
	public static class SpecFileReader
	{
		public static List<List<string>> ReadAllBlocks(string filename)
		{
			return ReadAllBlocks(File.OpenText(filename));
		}
		
		public static List<List<string>> ReadAllBlocks(StreamReader sr)
		{
			// Strip comments, and ignore any line of format 'something: blah blah'
			
			List<List<string>> blocks = new List<List<string>>();
			List<string> block = new List<string>();
			
			while (!sr.EndOfStream)
			{
				string l = sr.ReadLine();
				
				// Comments!
				int hash = l.IndexOf('#');
				if (hash > -1)
					l = l.Substring(0, hash);
				
				if (l.Contains("passthru:"))
					continue;
				
				if (l.StartsWith("\t")) // if it's indented, add it to the current block!
					block.Add(l);
				else if ((l.Contains("(")) // not indented & has a bracket in it? (for gl.spec)
				         || l.Contains(" enum:") || l.Contains(" define:")) // not indented & contains ' enum:'? (for enum.spec)
				{
					// Must be the start of a new block!
					if (block.Count > 0)
						blocks.Add(new List<string>(block));
					block.Clear();
					
					block.Add(l);
				}
			}
			
			// Last block?
			if (block.Count > 0)
				blocks.Add(new List<string>(block));
			
			return blocks;
		}
	}
}

